﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using Microsoft.SharePoint;
using ProjectHT.YKDGVB2012.Core.Datacontract;

namespace ProjectHT.YKDGVB2012.Core.Hanlder
{
    public class MainHanlder: IHttpHandler
    {
        private IMainList _mainlist;
        private IComment _Icomment;
        public MainHanlder()
        {

            var mainservice = new MainServices(SPContext.GetContext(HttpContext.Current));
            _mainlist = mainservice;
            var comment = new CommentServices(SPContext.Current);
            _Icomment = comment;
        }

        public void ProcessRequest(HttpContext context)
        {
            var request = context.Request.RequestType;
            switch (request)
            {
                case "POST":
                    Postmethod(context);
                    break;
                case "GET":
                    Getmethod(context);
                    break;
             }


        }

        private void Getmethod(HttpContext context)
        {


            
        }

        private void Postmethod(HttpContext context)
        {

            var act = String.Format("{0}", context.Request.Params[Helper.Constans.ACTION]);
            if(!String.IsNullOrEmpty(act))
            {
                switch (act)
                {
                    case "del":
                        DeleteMain(context);
                        break;
                    case "cmd":
                        DeleteComment(context);
                        break;
                }
            }
        }

        private void DeleteComment(HttpContext context)
        {
            var itemid = String.Format("{0}", context.Request.Params[Helper.Constans.ITEMID]);
            if (!string.IsNullOrEmpty(itemid))
            {
                var id = Common.Ultity.Splist(itemid, ",");

                for (int i = 0; i < id.Length - 1; i++)
                {
                    if (!string.IsNullOrEmpty(id[i]))
                    {
                        _Icomment.Delete(Convert.ToInt32(id[i].Trim()), SPContext.GetContext(context));
                    }
                }
                var mesage = new Common.Messager { Statust = "true", content = "Dữ liệu đã được xóa" };
                var json = new JavaScriptSerializer();
                var build = new StringBuilder();
                json.Serialize(mesage, build);
                context.Response.ContentType = "application/Json,Utf-8";
                context.Response.Write(build.ToString());

            }
        }

        private void DeleteMain(HttpContext context)
        {
            var itemid = String.Format("{0}",context.Request.Params[Helper.Constans.ITEMID]);
            if(!string.IsNullOrEmpty(itemid))
            {
                var id = Common.Ultity.Splist(itemid, ",");

                for (int i = 0; i < id.Length-1; i++)
                {
                    if(!string.IsNullOrEmpty(id[i]))
                    {
                        _mainlist.DeleteItem(Convert.ToInt32(id[i].Trim()),SPContext.GetContext(context));                       
                    }                    
                }
                var mesage = new Common.Messager { Statust = "true", content = "Dữ liệu đã được xóa" };
                var json = new JavaScriptSerializer();
                var build = new StringBuilder();
                json.Serialize(mesage, build);
                context.Response.ContentType = "application/Json,Utf-8";
                context.Response.Write(build.ToString());
              
            }
        }

        public bool IsReusable { get { return false; } }
    }
}
